package com.zhb.cloud.generate;

import java.util.List;
import java.util.Map;

import com.zhb.cloud.template.CamleNameUtil;
import com.zhb.cloud.template.FreeMarkertUtil;
import com.zhb.cloud.template.TableAndColumnUtil;
import com.zhb.cloud.template.TemplateConstant;

/**
 * @ClassName: GenerateCode
 * @Description: 代码生成器
 * @author zhb
 * @date 2016年1月14日 下午4:20:14 * @param templatePath 模板文件存放目录
 * @param templateName
 *            模板文件名称
 * @param root
 *            数据模型根对象
 * @param templateEncoding
 *            模板文件的编码方式
 * @param out
 *            输出流
 * 
 */
public class GenerateCode {
	public static void main(String[] args) throws Exception {
		List<String> tableList = TableAndColumnUtil.getTables();
		for (String table : tableList) {
			Map<String, Object> params = FreeMarkertUtil.getParams(table);
			// 实体名称
			String modelName = CamleNameUtil.camelNameWithoutProfix(table);
			// 首字母小写的实体名称
			String camelModelName = CamleNameUtil.camelNameProfixLower(table);
			params.put("modulePackageName", "com.zhb.cloud");
			params.put("authorName", "zhb");
			params.put("modelName", modelName);
			params.put("camelModelName", camelModelName);
			// 信息页面遍历
			params.put("modelList", camelModelName + "List");
			// 序号
			params.put("modelStat", camelModelName + "Stat");
			params.put("prefix", "$");
			params.put("module", "privilege");

			FreeMarkertUtil.generate(
					TemplateConstant.TEMPLATE_CONTROLLER, params, TemplateConstant.OUTPUT_DIRECTORY
							+ TemplateConstant.CONTROLLER_DIRECTORY + camelModelName + "/controller",
					modelName + "Controller.java");
			FreeMarkertUtil.generate(
					TemplateConstant.TEMPLATE_SERVICE, params, TemplateConstant.OUTPUT_DIRECTORY
							+ TemplateConstant.SERVICE_DIRECTORY + camelModelName + "/service",
					modelName + "Service.java");
			FreeMarkertUtil.generate(
					TemplateConstant.TEMPLATE_SERVICE_IMPL, params, TemplateConstant.OUTPUT_DIRECTORY
							+ TemplateConstant.SERVICE_IMPL_DIRECTORY + camelModelName + "/service/impl",
					modelName + "ServiceImpl.java");
			/**
			 * 页面和JS
			 */
			FreeMarkertUtil.generate(TemplateConstant.TEMPLATE_MODEL_HTML, params,
					TemplateConstant.HTML_OUTPUT_DIRECTORY + camelModelName, camelModelName + "Info.html");
			FreeMarkertUtil.generate(TemplateConstant.TEMPLATE_MODEL_JS, params,
					TemplateConstant.JS_OUTPUT_DIRECTORY + camelModelName, camelModelName + ".js");
			FreeMarkertUtil.generate(TemplateConstant.TEMPLATE_ADD_HTML, params,
					TemplateConstant.HTML_OUTPUT_DIRECTORY + camelModelName, "add.html");
			FreeMarkertUtil.generate(TemplateConstant.TEMPLATE_ADD_JS, params,
					TemplateConstant.JS_OUTPUT_DIRECTORY + camelModelName, "add.js");
			FreeMarkertUtil.generate(TemplateConstant.TEMPLATE_EDIT_HTML, params,
					TemplateConstant.HTML_OUTPUT_DIRECTORY + camelModelName, "edit.html");
			FreeMarkertUtil.generate(TemplateConstant.TEMPLATE_EDIT_JS, params,
					TemplateConstant.JS_OUTPUT_DIRECTORY + camelModelName, "edit.js");
			FreeMarkertUtil.generate(TemplateConstant.TEMPLATE_DELETE_HTML, params,
					TemplateConstant.HTML_OUTPUT_DIRECTORY + camelModelName, "delete.html");
		}
	}
}
